package com.futures.investm.fragment;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.futures.investm.R;
import com.futures.investm.adapter.CnFutureAdapter;
import com.futures.investm.constant.Api;
import com.futures.investm.entity.CnFutures;
import com.futures.investm.entity.Future;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.vise.xsnow.http.ViseHttp;
import com.vise.xsnow.http.callback.ACallback;
import com.vise.xsnow.http.request.GetRequest;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import tech.com.commoncore.base.BaseFragment;
import tech.com.commoncore.entity.BaseTResp;

import static com.futures.investm.utils.ListToStringUtil.listToString;


public class ChinaMarketFragment extends BaseFragment {
    @BindView(R.id.rv_foreign)
    RecyclerView rv_foreign;


    //国内期货
    private CnFutureAdapter mCnFutureAdapter;

    @BindView(R.id.sm)
    SmartRefreshLayout sm;


    public static ChinaMarketFragment newInstance() {
        Bundle args = new Bundle();
        ChinaMarketFragment fragment = new ChinaMarketFragment();
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public int getContentLayout() {
        return R.layout.fragment_foreign;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        //国内
        rv_foreign.setLayoutManager(new LinearLayoutManager(mContext));
        mCnFutureAdapter = new CnFutureAdapter();
        getCnDatas();

        sm.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshLayout) {

                getCnDatas();

                sm.finishRefresh();
            }
        });

    }


    private void getCnDatas() {

        List<String> list = Arrays.asList(mContext.getResources().getStringArray(R.array.FutureParam));

        GetRequest request = ViseHttp.GET(Api.CN_FUTURES)
                .addParam("list", listToString(list));
        request.request(new ACallback<BaseTResp<CnFutures>>() {
            @Override
            public void onSuccess(BaseTResp<CnFutures> data) {

                if (data != null) {
                    CnFutures data1 = data.data;
                    List<Future> futures = new ArrayList<>();
                    futures.add(new Future("SC0", data1.SC0.get(0), data1.SC0));
                    futures.add(new Future("HC0", data1.HC0.get(0), data1.HC0));
                    futures.add(new Future("BU0", data1.BU0.get(0), data1.BU0));
                    futures.add(new Future("AU0", data1.AU0.get(0), data1.AU0));
                    futures.add(new Future("AG0", data1.AG0.get(0), data1.AG0));
                    futures.add(new Future("ZN0", data1.ZN0.get(0), data1.ZN0));

                    mCnFutureAdapter.setNewData(futures);

                }


            }

            @Override
            public void onFail(int errCode, String errMsg) {
            }
        });
        rv_foreign.setAdapter(mCnFutureAdapter);
    }


}
